﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Deanery.Controller.Directories
{
    class FacultyController
    {
        private readonly deaneryEntities _db = new deaneryEntities();
        
        /// <summary>
        /// This method transfers columns name in ukrainian for view table Faculty
        /// </summary>
        /// <returns>Array of string with columns names</returns>
        public Array ColumnName()
        {
            string[] columnName = { "ID", "Факультет", "Декан" };
            return columnName;
        }

        /// <summary>
        /// This method transfers columns data type in table Faculty
        /// </summary>
        /// <returns>Array of string with columns data type</returns>
        public Array ColumnValueType()
        {
            string[] columnDataType = { "guid", "string", "guid" };
            return columnDataType;
        }

        /// <summary>
        /// This method select all reccords from table Faculty
        /// PROBLEM: if will be many reccords in this table, method can slove the application
        /// SOLVE: stable quantities assync selection 
        /// </summary>
        /// <returns>Set of IQueryable</returns>
        public IQueryable GetAll()
        {
            var list = _db.Faculties.Join(_db.Teachers, f => f.id_teacher, t => t.id_teacher, (f, t) => new
                {
                    f.id_faculty,
                    f.nv_faculty_name,
                    dekan = (t.nv_first_name + " " + t.nv_last_name + " " + t.nv_middle_name)
                });
            return list;
        }

        /// <summary>
        /// This method select all tests name from table Faculty
        /// idea of this method: to return data from all conected tables with this one
        /// </summary>
        /// <returns>Array(every item is test name)</returns>
        public Array GetConectedData()
        {
            var list = from t in _db.Teachers select t.nv_first_name + " " + t.nv_last_name + " " + t.nv_middle_name; 
            return list.ToArray();
        }


        /// <summary>
        /// This method performs INSERT one record to DB in table Faculty
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to insert</param>
        public void Insert(ArrayList reccord)
        {
            var obj = new Faculty
            {
                id_faculty = GenerateGuid(),
                nv_faculty_name = reccord[0].ToString(),
                id_teacher = GetTeacher(reccord[1].ToString())
            };
            _db.Faculties.AddObject(obj);
            _db.SaveChanges();
        }

        /// <summary>
        /// This method performs UPDATE one record to DB in table Faculty
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="updateGuid">guid value to search reccord in table</param>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to update</param>
        public void Update(Guid updateGuid, ArrayList reccord)
        {
            var obj = (_db.Faculties.Where(t => t.id_faculty == updateGuid)).Single();
            obj.nv_faculty_name = reccord[0].ToString();
            obj.id_teacher = GetTeacher(reccord[1].ToString());
            _db.SaveChanges();
        }

        /// <summary>
        /// This method returns the guid of Dean
        /// </summary>
        /// <param name="name">string in the name of Dean</param>
        /// <returns>guid</returns>
        private Guid GetTeacher(string name)
        {
            var elem = name.Split(' ');
                var first = elem[0];
                var last = elem[1];
                var middle = elem[2];
            var element = _db.Teachers.Where(t => t.nv_first_name == first && t.nv_last_name == last &&  t.nv_middle_name == middle).
                Select(t => t.id_teacher).Single();
            return element;
        }

        /// <summary>
        /// This method generates new guid
        /// </summary>
        /// <returns>New guid</returns>
        public Guid GenerateGuid()
        {
            var guid = Guid.NewGuid();
            return guid;
        }

        /// <summary>
        /// This method select guid from table Faculty
        /// </summary>
        /// <param name="name">String value for search guid in table</param>
        /// <returns>Guid of passed parameter</returns>
        public Guid FindGuidByName(string name)
        {
            var guid = _db.Faculties.Where(t => t.nv_faculty_name == name).Select(t => t.id_faculty).Single();
            return guid;
        }
    }
}
